/* pipedemo.c
 * purpose: learn how to use pipe to programming
 * create by 2018-12-17
 * copyright@ ChengFeitian
 */

#include <stdio.h>
#include <unistd.h>

main()
{
	int len,i,apipe[2];	//two file descriptors
	char buf[BUFSIZ];	//for reading end

	//get a pipe
	if(pipe(apipe) == -1)
	{
		perror("could not make pipe");
		exit(1);
	}

	printf("Got a pipe!It is file descriptors:{%d %d}\n",apipe[0],apipe[1]);
	
	while(fgets(buf,BUFSIZ,stdin))
	{
		len = strlen(buf);
		if(write(apipe[1],buf,len) != len)
		{
			perror("writing to pipe");
			break;
		}
		for(i = 0;i<len;i++)
			buf[i] = 'X';
		len = read(apipe[0],buf,BUFSIZ);
		if(len == -1)
		{
			perror("reading from pipe");
			break;
		}
		if(write(1,buf,len) != len)
		{
			perror("writing to stdout");
			break;
		}
	}
}
